﻿using System;
using System.Net.Mail;
using System.Web.Mvc;
using ServiceClient;
using WebMvc.Dao;
using WebMvc.Models;

namespace WebMvc.Controllers
{
    public class SalasController : Controller
    {
        private readonly SalaDao _salaDao;
        private readonly TicketDao _ticketDao;
        private readonly OpenMeetingsServiceClient _serviceClient;

        public SalasController(SalaDao salaDao, TicketDao ticketDao, OpenMeetingsServiceClient serviceClient)
        {
            if (salaDao == null)
                throw new ArgumentNullException("salaDao");

            if (ticketDao == null)
                throw new ArgumentNullException("ticketDao");

            if (serviceClient == null)
                throw new ArgumentNullException("serviceClient");

            _salaDao = salaDao;
            _ticketDao = ticketDao;
            _serviceClient = serviceClient;
        }
        
        // GET: /Salas/
        public ActionResult Index()
        {
            return View(_salaDao.GetAll());
        }

        //
        // GET: /Salas/Ver/5
        public ActionResult Ver(int id)
        {
            return View(_salaDao.GetById(id));
        }

        // GET: /Salas/Crear?ticketId=5
        public ActionResult Crear(int ticketId)
        {
            return View();
        }

        // POST: /Salas/Crear
        [HttpPost]
        public ActionResult Crear(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: /Salas/Invitar/5
        public ActionResult Invitar(int id)
        {
            ViewData["UrlInvitacion"] = null;
            return View(_salaDao.GetById(id));
        }

        // POST: /Salas/Invitar/
        [HttpPost]
        public ActionResult Invitar(Sala sala)
        {
            var s = _salaDao.GetById(sala.Id);

            var nombre = Request.Form["nombreUsr"];
            var apellido = Request.Form["apellidoUsr"];
            var email = Request.Form["email"];
            var moderador = Request.Form["moderador"].Contains("true");

            ViewData["UrlInvitacion"] = _serviceClient.GetInvitationHash(s, nombre, apellido, email, moderador);
            return View(s);
        }
        
        //
        // GET: /Salas/Edit/5
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Salas/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Salas/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Salas/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
